﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

/// <summary>
/// Recvers 的摘要说明
/// </summary>
public class Recvers
{
    public enum UserType {Admin,Doctor,Student,Team,Expert,ExpertStudent }
    public class User
    {
        public int Guid;
        public string UserID;
        public string RealName;
        public UserType UT;
        public ArrayList Appends;
        public string ToHTML(bool display)
        {
            string html = "<div " + (display ? "" : " style='padding-left:18px;display:none;'") + "><p>";
            if (this.Appends != null)
            {
                html += "<img src='" + Params.ImagesFolderHttp + "mylist_open_btn.gif' onclick='OpenNextDiv(this)' />";
            }
            html += "<input type='checkbox' guid='"+this.Guid.ToString()+"'/>";
            switch (this.UT)
            {
                case UserType.Admin:
                    html += "管理员";
                    break;
                case UserType.Doctor:
                    html += "导师：" + this.RealName;
                    break;
                case UserType.Student:
                    html += "学生：" + this.RealName;
                    break;
                case UserType.Team:
                    html += "团队：" + this.RealName;
                    break;
                case UserType.Expert:
                    html += "评审专家" ;
                    break;
                case UserType.ExpertStudent:
                    html += "论文："+this.RealName;
                    break;
            }
            if (this.Appends != null)
            {
                html += " <input type='checkbox' guid='0'/>选择全部";
            }
            html += "</p>";
            if (this.Appends != null)
            {
                foreach (object obj in this.Appends)
                {
                    html += ((User)obj).ToHTML(false);
                }
            }
            html += "</div>";
            return html;
        }
    }

    public int Guid;
    public UserType UT;
    public ArrayList Ary;

    public Recvers()
    {
    }
	public Recvers(int g)
	{
        this.Init(g);
	}

    public void Init(int g)
    {
        this.Guid = g;
        LwUser lu = new LwUser(g);
        if (lu.UserType == LwUser.AdminType)
        {
            this.UT = UserType.Admin;
        }
        else if (lu.UserType == LwUser.TeamType)
        {
            this.UT = UserType.Team;
        }
        else if (lu.UserType == LwUser.DoctorType)
        {
            this.UT = UserType.Doctor;
        }
        else
        {
            this.UT = UserType.Student;
        }
        this.Ary = new ArrayList();
        switch (this.UT)
        {
            case UserType.Admin:
                this.Admin();
                break;
            case UserType.Doctor:
                this.Doctor();
                break;
            case UserType.Student:
                this.Student();
                break;
            case UserType.Team:
                this.Team();
                break;
        }
    }

    private void Admin()
    {
        string sql = "select * from Users where UserType='"+LwUser.TeamType+"' order by UserID asc";
        DataTable teDt = Fun.CreateDt(sql);
        if (teDt.Rows.Count < 1)
        {
            this.Ary = null;
            return;
        }
        foreach (DataRow teDr in teDt.Rows)
        {
            User t = new User();
            t.Guid = Convert.ToInt32(teDr["guid"]);
            t.RealName = teDr["RealName"].ToString().Trim();
            t.UserID = teDr["UserID"].ToString().Trim();
            t.UT = UserType.Team;
            sql = "select u.* from Doctor d Join Users u on d.UserGuid=u.Guid where d.TeamGuid="+t.Guid.ToString()+" order by u.UserID asc";
            DataTable docDt = Fun.CreateDt(sql);
            if (docDt.Rows.Count < 1)
            {
                t.Appends = null;
                this.Ary.Add(t);
                continue;
            }
            t.Appends = new ArrayList();
            foreach (DataRow docDr in docDt.Rows)
            {
                User d = new User();
                d.Guid = Convert.ToInt32(docDr["guid"]);
                d.RealName = docDr["RealName"].ToString().Trim();
                d.UserID = docDr["UserID"].ToString().Trim();
                d.UT = UserType.Doctor;
                sql = "select u.* from Student s Join Users u on s.UserGuid=u.Guid where s.DoctorGuid=" + d.Guid.ToString() + " order by u.UserID asc";
                DataTable stuDt = Fun.CreateDt(sql);
                if (stuDt.Rows.Count < 1)
                {
                    d.Appends = null;
                    t.Appends.Add(d);
                    continue;
                }
                d.Appends = new ArrayList();
                foreach (DataRow stuDr in stuDt.Rows)
                {
                    User s = new User();
                    s.Guid = Convert.ToInt32(stuDr["guid"]);
                    s.RealName = stuDr["RealName"].ToString().Trim();
                    s.UserID = stuDr["UserID"].ToString().Trim();
                    s.UT = UserType.Student;
                    s.Appends = null;
                    d.Appends.Add(s);
                }
                t.Appends.Add(d);
            }
            this.Ary.Add(t);
        }
    }

    private void Doctor()
    {
        string sql = "select top 1 * from Users where UserType='" + LwUser.AdminType + "'";
        DataRow adDr = Fun.CreateDr(sql);
        User a = new User();
        a.Guid = Convert.ToInt32(adDr["guid"]);
        a.RealName = adDr["RealName"].ToString().Trim();
        a.UserID = adDr["UserID"].ToString().Trim();
        a.UT = UserType.Admin;
        a.Appends = null;
        this.Ary.Add(a);
        sql = "select u.* from Doctor d Join Users u on d.TeamGuid=u.Guid where d.UserGuid=" + this.Guid.ToString();
        DataTable teaDt = Fun.CreateDt(sql);
        if (teaDt.Rows.Count > 0)
        {
            DataRow dr = teaDt.Rows[0];
            User t = new User();
            t.Guid = Convert.ToInt32(dr["guid"]);
            t.RealName = dr["RealName"].ToString().Trim();
            t.UserID = dr["UserID"].ToString().Trim();
            t.UT = UserType.Team;
            t.Appends = null;
            this.Ary.Add(t);
        }
        sql = "select u.* from Student s Join Users u on s.UserGuid=u.Guid where s.DoctorGuid=" + this.Guid.ToString() + " order by u.UserID asc";
        DataTable stuDt = Fun.CreateDt(sql);
        if (stuDt.Rows.Count >0)
        {
            foreach (DataRow stuDr in stuDt.Rows)
            {
                User s = new User();
                s.Guid = Convert.ToInt32(stuDr["guid"]);
                s.RealName = stuDr["RealName"].ToString().Trim();
                s.UserID = stuDr["UserID"].ToString().Trim();
                s.UT = UserType.Student;
                s.Appends = null;
                this.Ary.Add(s);
            }
        }
        sql = "select u.*,s.PaperName from Student s Join Users u on s.UserGuid=u.Guid where s.ExpertGuid=" + this.Guid.ToString() + " order by u.UserID asc";
        stuDt = Fun.CreateDt(sql);
        if (stuDt.Rows.Count >0)
        {
            foreach (DataRow stuDr in stuDt.Rows)
            {
                User s = new User();
                s.Guid = Convert.ToInt32(stuDr["guid"]);
                s.RealName = stuDr["PaperName"].ToString().Trim();
                s.UserID = stuDr["UserID"].ToString().Trim();
                s.UT = UserType.ExpertStudent;
                s.Appends = null;
                this.Ary.Add(s);
            }
        }
    }

    private void Student()
    {
        string sql = "select u.* from Student s Join Users u on s.DoctorGuid=u.Guid where s.UserGuid="+this.Guid.ToString();
        DataTable docDt = Fun.CreateDt(sql);
        if (docDt.Rows.Count > 0)
        {
            DataRow dr = docDt.Rows[0];
            User d = new User();
            d.Guid = Convert.ToInt32(dr["guid"]);
            d.RealName = dr["RealName"].ToString().Trim();
            d.UserID = dr["UserID"].ToString().Trim();
            d.UT = UserType.Doctor;
            d.Appends = null;
            this.Ary.Add(d);
        }
        sql = "select u.* from Student s Join Users u on s.ExpertGuid=u.Guid where s.UserGuid=" + this.Guid.ToString();
        DataTable expDt = Fun.CreateDt(sql);
        if (expDt.Rows.Count > 0)
        {
            DataRow dr = expDt.Rows[0];
            User e = new User();
            e.Guid = Convert.ToInt32(dr["guid"]);
            e.RealName = dr["RealName"].ToString().Trim();
            e.UserID = dr["UserID"].ToString().Trim();
            e.UT = UserType.Expert;
            e.Appends = null;
            this.Ary.Add(e);
        }
    }

    private void Team()
    {
        string sql = "select top 1 * from Users where UserType='"+LwUser.AdminType+"'";
        DataRow adDr = Fun.CreateDr(sql);
        User a = new User();
        a.Guid = Convert.ToInt32(adDr["guid"]);
        a.RealName = adDr["RealName"].ToString().Trim();
        a.UserID = adDr["UserID"].ToString().Trim();
        a.UT = UserType.Admin;
        a.Appends = null;
        this.Ary.Add(a);
        sql = "select u.* from Doctor d Join Users u on d.UserGuid=u.Guid where d.TeamGuid=" + this.Guid.ToString() + " order by u.UserID asc";
        DataTable docDt = Fun.CreateDt(sql);
        if (docDt.Rows.Count < 1)
        {
            return;
        }
        foreach (DataRow docDr in docDt.Rows)
        {
            User d = new User();
            d.Guid = Convert.ToInt32(docDr["guid"]);
            d.RealName = docDr["RealName"].ToString().Trim();
            d.UserID = docDr["UserID"].ToString().Trim();
            d.UT = UserType.Doctor;
            sql = "select u.* from Student s Join Users u on s.UserGuid=u.Guid where s.DoctorGuid=" + d.Guid.ToString() + " order by u.UserID asc";
            DataTable stuDt = Fun.CreateDt(sql);
            if (stuDt.Rows.Count < 1)
            {
                d.Appends = null;
                this.Ary.Add(d);
                continue;
            }
            d.Appends = new ArrayList();
            foreach (DataRow stuDr in stuDt.Rows)
            {
                User s = new User();
                s.Guid = Convert.ToInt32(stuDr["guid"]);
                s.RealName = stuDr["RealName"].ToString().Trim();
                s.UserID = stuDr["UserID"].ToString().Trim();
                s.UT = UserType.Student;
                s.Appends = null;
                d.Appends.Add(s);
            }
            this.Ary.Add(d);
        }
    }

    public string ToHTML()
    {
        string html = "";
        foreach (object obj in this.Ary)
        {
            User u = (User)obj;
            html += u.ToHTML(true);
        }
        return html;
    }
}
